package br.com.pixt;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class DataHelper {

   private static final String DATABASE_NAME = "ZAE.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "folhas";

   private Context context;
   private SQLiteDatabase db;

   private SQLiteStatement insertStmt;
   
   
   private static final String INSERT = "insert into "
      + TABLE_NAME + "(INT_ID_CLIENTE,INT_ID_VEICULO,INT_ID_LOGRADOURO,INT_QTDE_HS,CHA_CELULAR_DDD,VAR_CELULAR_NUMERO,DAT_INICIO) values (?,?,?,?,?,?,?)";  

   public DataHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
     // openHelper.onUpgrade(this.db, 0, 0);
      this.insertStmt = this.db.compileStatement(INSERT);
   }

   public long insert(int INT_ID_CLIENTE,int INT_ID_VEICULO,int INT_ID_LOGRADOURO,int INT_QTDE_HS,String CHA_CELULAR_DDD,
		              String VAR_CELULAR_NUMERO,String DAT_INICIO) {
      this.insertStmt.bindLong(1, INT_ID_CLIENTE);
      this.insertStmt.bindLong(2, INT_ID_VEICULO);
      this.insertStmt.bindLong(3, INT_ID_LOGRADOURO);
      this.insertStmt.bindLong(4, INT_QTDE_HS);
      this.insertStmt.bindString(5, CHA_CELULAR_DDD);
      this.insertStmt.bindString(6, VAR_CELULAR_NUMERO);
      this.insertStmt.bindString(7, DAT_INICIO);
      return this.insertStmt.executeInsert();
   }

   public void deleteAll() {
      this.db.delete(TABLE_NAME, null, null);
   }
   
 
   public long update(Folha f){  
       ContentValues cv = new ContentValues();
       
 	   cv.put("INT_ID_VEICULO",f.idVeiculo);      
       cv.put("INT_ID_LOGRADOURO", f.idLogradouro);  
       cv.put("INT_QTDE_HS", f.lonQtdeHoras);

       if (f.strCelularDDD != null)
    	   cv.put("CHA_CELULAR_DDD", f.strCelularDDD);  
       
       if (f.strCelularNumero != null)
    	   cv.put("VAR_CELULAR_NUMERO", f.strCelularNumero);  
       
       if (f.strDtInicio != null)
    	   cv.put("DAT_INICIO", f.strDtInicio);  
   
       return db.update(TABLE_NAME, cv, "1=1",null);  
   }  
   
 
   public int delete(int id){  
       return db.delete(TABLE_NAME, "_id = ?",   
           new String[]{ String.valueOf(id) });  
   }  

   public String select(String wrd) {
      String list = new String();
      Cursor cursor = this.db.query(TABLE_NAME, new String[] {"INT_ID_CLIENTE,INT_ID_VEICULO,INT_ID_LOGRADOURO,INT_QTDE_HS,CHA_CELULAR_DDD,VAR_CELULAR_NUMERO,DAT_INICIO"},null, null, null, null, null);
      if (cursor.moveToFirst()) {
         do {
            list = cursor.getString(0);
         } while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      return list;
   }

   public List selectAll() {
              List list = new ArrayList();
              Cursor cursor = this.db.query(TABLE_NAME, new String[] {"INT_ID_CLIENTE,INT_ID_VEICULO,INT_ID_LOGRADOURO,INT_QTDE_HS,CHA_CELULAR_DDD,VAR_CELULAR_NUMERO,DAT_INICIO" }, null, null, null, null, "INT_ID_FOLHA desc");
              if (cursor.moveToFirst()) {
                 do {
                    list.add(cursor.getString(0));
                    list.add(cursor.getString(1));
                    list.add(cursor.getString(2));
                    list.add(cursor.getString(3));
                    list.add(cursor.getString(4));
                    list.add(cursor.getString(5));
                    list.add(cursor.getString(6));
                 } while (cursor.moveToNext());
              }
              if (cursor != null && !cursor.isClosed()) {
                 cursor.close();
              }
              return list;
           }

   private static class OpenHelper extends SQLiteOpenHelper {

      OpenHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

//		 "CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT, count INTEGER)"

      @Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL(
        		 "CREATE TABLE " + TABLE_NAME + "(INT_ID_FOLHA integer primary key autoincrement, " +
			"INT_ID_FOLHA_ANTERIOR integer null," +
			"INT_ID_CLIENTE integer null," +
			"INT_ID_VEICULO integer null,"+
			"INT_ID_LOGRADOURO integer null,"+  
			"INT_QTDE_HS integer null,"+
			"CHA_CELULAR_DDD text null,"+
			"VAR_CELULAR_NUMERO text null,"+
			"DAT_INICIO text null,"+
			"DAT_FIM text null,"+
			"BIT_FINALIZADO integer null,"+
			"DAT_FINALIZADO text null,"+
			"BIT_MULTADO integer null,"+
			"DAT_MULTADO text null,"+
			"INT_ID_AGENTE integer null)"
        		 );
      }

      @Override
  	  public void onUpgrade(SQLiteDatabase db, int oldVersion, int ewVersnion) {
         Log.w("banco Database", "Upgrading database, this will destroy the database and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
      }
   

	}


}